Bitcoin Scriptで遊ぶ(2+4=6)
BitcoinのScriptでよく例にある 2 + 4 = 6 を実際にやってみた
今回は ? + 4 = 6 で ? の値がわかればUTXOが使用できる用にした
まず+ 4 = 6のredeem_scriptを作成する
code:+ 4 = 6
redeem_script = Bitcoin::Script.from_string("OP_4 OP_ADD OP_6 OP_EQUAL")
p2sh_address = Bitcoin::Script.new(Bitcoin::Script.to_p2sh_script(Bitcoin.hash160(redeem_script.to_payload.bth))).get_p2sh_address
"2MwrkcFztNH6EN9jKu6rLUiBtKmVfjjoCrp"
2MwrkcFztNH6EN9jKu6rLUiBtKmVfjjoCrpこのアドレスに対して送金をする
次にこのUTXOを使用するためにトランザクションを作成する
code:tx
tx = Bitcoin::Protocol::Tx.new
tx_in = Bitcoin::Protocol::TxIn.from_hex_hash("e0a48569cd03a7362b1e547bde79c35a64a424244e8d76cfb47ae7405d028a9c", 1)
tx.add_in(tx_in)
tx_out = Bitcoin::Protocol::TxOut.value_to_address(900, "2Msn3S5T3zXJjGdiaF8iAUj1k4Z8Yme649p")
tx.add_out(tx_out)
script_sigに? + 4 = 6の?部分である2とredeem_scriptを入れる
code:script_sig
script_sig = Bitcoin::Script.new(Bitcoin::Script.from_string("OP_2").to_payload + Bitcoin::Script.pack_pushdata(redeem_script.to_payload))
tx.in0.script_sig = script_sig.to_payload raw_transaction = tx.to_payload.bth
>01000000019c8a025d40e77ab4cf768d4e2424a4645ac379de7b541e2b36a703cd6985a4e00100000006520454935687ffffffff01840300000000000017a91405d4156df025d97e1ac33fcb1e4a1785ef159fb88700000000
raw_transactionを送金する
code:送金
~ $bcli-drt 01000000019c8a025d40e77ab4cf768d4e2424a4645ac379de7b541e2b36a703cd6985a4e00100000006520454935687ffffffff01840300000000000017a91405d4156df025d97e1ac33fcb1e4a1785ef159fb88700000000
{
"txid": "9adab3c70fc6086e98db7d568d77a741fce81b0bd8621e54405a71f6dc6b05fb",
"hash": "9adab3c70fc6086e98db7d568d77a741fce81b0bd8621e54405a71f6dc6b05fb",
"version": 1,
"size": 89,
"vsize": 89,
"weight": 356,
"locktime": 0,
"vin": [
{
"txid": "e0a48569cd03a7362b1e547bde79c35a64a424244e8d76cfb47ae7405d028a9c",
"vout": 1,
"scriptSig": {
"asm": "2 -123114324",
"hex": "520454935687"
},
"sequence": 4294967295
}
],
"vout": [
{
"value": 0.00000900,
"n": 0,
"scriptPubKey": {
"asm": "OP_HASH160 05d4156df025d97e1ac33fcb1e4a1785ef159fb8 OP_EQUAL",
"hex": "a91405d4156df025d97e1ac33fcb1e4a1785ef159fb887",
"reqSigs": 1,
"type": "scripthash",
"addresses": [
"2Msn3S5T3zXJjGdiaF8iAUj1k4Z8Yme649p"
]
}
}
]
}
~ $bcli-srt 01000000019c8a025d40e77ab4cf768d4e2424a4645ac379de7b541e2b36a703cd6985a4e00100000006520454935687ffffffff01840300000000000017a91405d4156df025d97e1ac33fcb1e4a1785ef159fb88700000000
9adab3c70fc6086e98db7d568d77a741fce81b0bd8621e54405a71f6dc6b05fb
p2wsh_address = Bitcoin::Script.to_p2wsh(redeem_script).to_addr
鍵が無くても使用できるようにするのは危険だけど面白いことができるかも
Bitcoin.encode_base58_address("1c0e61220788ead488be2a57651f36a6e7033375", "c4")
bitcoinrbの場合
code: ? + 4 = 6
require 'bitcoin'
Bitcoin.chain_params = :testnet
redeem_script = Bitcoin::Script.from_string("OP_4 OP_ADD OP_6 OP_EQUAL")
redeem_script.to_p2sh.to_h
Bitcoin.encode_base58_address("32989f4fa2ebb2c87fa7942ef521c54b2847a08b", "c4")
tx = Bitcoin::Tx.new
tx_in = Bitcoin::TxIn.new(out_point: out_point)
out_point = Bitcoin::OutPoint.from_txid("txid", "index")
tx_out = Bitcoin::TxOut.new(value: value, script_pubkey: script_pubkey)
amount = "amount(satoshi)"
value = amount - "fee"
script_pubkey = Bitcoin::Script.parse_from_addr("address")
script = Bitcoin::Script.from_string("OP_2").to_payload + Bitcoin::Script.pack_pushdata(redeem_script.to_payload)
script_sig = Bitcoin::Script.parse_from_payload(script)
tx.in0.script_sig = script_sig